import 'package:flutter/material.dart';
import 'package:flutter_project/common/route_page/route_name.dart';
import 'package:flutter_project/config/common_string_config.dart';
import 'package:flutter_project/user/user_profile_page.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../add_publish/publish_add_main_page.dart';
import '../config/theme_color.dart';
import '../generated/l10n.dart';
import '../picture/picture_main_page.dart';
import '../requst/dio_http_utils.dart';
import '../user/user_login/user_info_model.dart';
import 'home_page.dart';

class AppMainPage extends StatefulWidget {
  const AppMainPage({super.key});

  @override
  State<AppMainPage> createState() => _AppMainPageState();
}

class _AppMainPageState extends State<AppMainPage> {
  int _bottomNavIndex = 0;
  final _checkColor = ThemeColor.mainColor;
  final _normalColor = ThemeColor.textColor;
  List<String> titleList = [];
  List<Icon> iconList = [
    const Icon(Icons.home),
    const Icon(Icons.person),
  ];

  ValueNotifier<UserInfoRespModel> infoRespModel =
      ValueNotifier<UserInfoRespModel>(UserInfoRespModel());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      endDrawer: ValueListenableBuilder(
          valueListenable: infoRespModel,
          builder:
              (BuildContext context, UserInfoRespModel value, Widget? child) {
            return UserProfilePage(
              infoRespModel: infoRespModel.value,
            );
          }),
      body: IndexedStack(
        index: _bottomNavIndex,
        children: const [
          HomePage(),
          PictureMainPage(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _bottomNavIndex,
        onTap: (int index) {
          setState(() {
            _bottomNavIndex = index;
          });
        },
        type: BottomNavigationBarType.fixed,
        selectedIconTheme: IconThemeData(color: _checkColor),
        unselectedIconTheme: IconThemeData(color: _normalColor),
        selectedItemColor: _checkColor,
        unselectedItemColor: _normalColor,
        selectedFontSize: 12,
        unselectedFontSize: 12,
        items: [
          _buildTabs(icon: iconList.first, titleStr: titleList.first),
          _buildTabs(icon: iconList.last, titleStr: titleList.last),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        backgroundColor: _checkColor,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(46),
        ),
        onPressed: () {
          RouteNames.push(const PublishAddMainPage());
        },
        child: const Icon(
          Icons.add,
          color: Colors.white,
        ),
      ),

      ///将FloatActionButton 与 BottomAppBar 融合到一起
      floatingActionButtonLocation:
          FloatingActionButtonLocation.miniCenterDocked,
    );
  }

  BottomNavigationBarItem _buildTabs(
      {required Icon icon, required String titleStr}) {
    return BottomNavigationBarItem(
      icon: icon,
      label: titleStr,
    );
  }

  @override
  void initState() {
    super.initState();
     titleList=[S.of(Get.context!).home,S.of(Get.context!).my];
    getUserInfo();
  }

  getUserInfo() async {
    var res = await HttpDioUtils.requestGetUrl(apiUrl: 'getUserInfo');
    if (res != null && res['ErrCode'] == 0) {
      UserInfoModel infoModel = UserInfoModel.fromJson(res);
      infoRespModel.value = infoModel.resp ?? UserInfoRespModel();
      String nickName = infoRespModel.value.nickname ?? '';
      if (nickName.isNotEmpty) {
        CommonStringConfig.nickName = nickName;
        SharedPreferences preferences = await SharedPreferences.getInstance();
        preferences.setString('nickName', infoRespModel.value.nickname ?? '');
      }
    }
  }
}
